<template>
  <div class="app-container">

   <!--查询-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="品种" prop="cropTypeVariety">
        <el-select v-model="queryParams.cropTypeVariety" placeholder="品种" clearable>
          <el-option
            v-for="dict in dict.type.sys_crop_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="健康状况" prop="healthStatus">
        <el-select v-model="queryParams.healthStatus" placeholder="健康状况" clearable>
          <el-option
            v-for="dict in dict.type.sys_grow_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crop:cropType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crop:cropType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crop:cropType:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crop:cropType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--列表-->
    <el-table v-loading="loading" :data="cropTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="农作物编号" align="center" prop="cropCode" />
      <el-table-column label="农作物名称" align="center" prop="cropName" />
      <el-table-column label="品种" align="center" prop="cropTypeVariety" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_crop_type" :value="scope.row.cropTypeVariety"/>
        </template>
      </el-table-column>
      <el-table-column label="引进类别数量" align="center" prop="num" />
      <el-table-column label="健康状况" align="center" prop="healthStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_grow_status" :value="scope.row.healthStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crop:cropType:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crop:cropType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农作物类别对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">

        <el-form-item label="农作物名称" prop="cropName">
          <el-select v-model="form.cropName" placeholder="请选择">
            <el-option
              v-for="item in cropList"
              :key="item.name"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="品种" prop="cropTypeVariety">
          <el-select v-model="form.cropTypeVariety" placeholder="品种" clearable>
            <el-option
              v-for="dict in dict.type.sys_crop_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="引进数量" prop="num">
          <el-input-number v-model="form.num" @change="handleChange" :min="1" label="描述文字"></el-input-number>
        </el-form-item>

        <el-form-item label="健康状况" prop="healthStatus">
          <el-select v-model="form.healthStatus" placeholder="健康状况" clearable>
            <el-option
              v-for="dict in dict.type.sys_grow_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="引进时间">
          <el-date-picker
            v-model="form.inTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCropType, getCropType, delCropType, addCropType, updateCropType} from "@/api/material/type";
import {listCrop} from "@/api/crop/products"
export default {
  name: "CropType",
  dicts: ['sys_normal_disable','sys_crop_type','sys_crop_plant','sys_crop_workshop',
    'sys_fertilizer','sys_crop_status','sys_grow_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农作物类别表格数据
      cropTypeList: [],
      // 农作物表格数据
      cropList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cropTypeVariety: undefined,
        healthStatus: undefined,
      },
      // 表单参数
      form: {
        num: 1,
      },
      // 表单校验
      rules: {
        farmerName: [
          { required: true, message: "农户名称不能为空", trigger: "blur" }
        ],
        person: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询农作物类别列表 */
    getList() {
      this.loading = true;
      listCropType(this.queryParams).then(response => {
        this.cropTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询农作物列表 */
    getCropList() {
      this.loading = true;
      listCrop().then(response => {
        this.cropList = response.rows;
        //this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        cropTypeId: undefined,
        cropCode: undefined,
        cropName: undefined,
        cropTypeVariety: undefined,
        num: 1,
        healthStatus: undefined,
        inTime: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cropTypeId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getCropList();
      this.open = true;
      this.title = "添加农作物类别";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cropTypeId = row.cropTypeId || this.ids
      getCropType(cropTypeId).then(response => {
        this.form = response.data;
        this.getCropList();
        this.open = true;
        this.title = "修改农作物类别";
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cropTypeId != undefined) {
            updateCropType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCropType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const cropTypeIds = row.cropTypeId || this.ids;
      this.$modal.confirm('是否确认删除农作物类别编号为"' + cropTypeIds + '"的数据项？').then(function() {
        return delCropType(cropTypeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('crop/cropType/export', {
        ...this.queryParams
      }, `cropType_${new Date().getTime()}.xlsx`)
    },

    /** 计数器 */
    handleChange(value) {
      console.log(value);
    },
  }
};
</script>
